﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.DTOs;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Butcher.winform.EnterpriseForms
{
    public partial class ApproachList : Form
    {
        public class Info
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
        public ApproachList()
        {
            InitializeComponent();
            dt_ApproachTime.Text = null;
            textBox1.Text = "";
            textBox3.Text = "";
            cl_Status.Text = Convert.ToString(-1);
            IList<Info> infoList = new List<Info>();
            Info info0 = new Info() { Id = -1, Name = "请选择" };
            Info info1 = new Info() { Id = 0, Name = "未作废" };
            Info info2 = new Info() { Id = 1, Name = "已作废" };
            infoList.Add(info0);
            infoList.Add(info1);
            infoList.Add(info2);
            cl_Status.DataSource = infoList;
            cl_Status.ValueMember = "Id";
            cl_Status.DisplayMember = "Name";
            ApproachListStr();
        }

        public class ApproachSearch
        {
            public DateTime? ApproachTime { get; set; }
            public string? Suppliers { get; set; }
            public string? SettlementType { get; set; }
            public int ApproachStatus { get; set; }
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
        }
        ApproachSearch showapp = new ApproachSearch();
        public DateTime ApproachTime = DateTime.Now;
        public string Suppliers = "";
        public string SettlementType = "";
        public int ApproachStatus = 0;
        public int pageIndex = 1;
        public int pageSize = 5;
        public int totalCount = 0;
        public int pageCount = 0;
        public void ApproachListStr()
        {
            string url = @"http://localhost:5229/api/Enterprise/SelectApproachList";
            HttpClient client = new HttpClient();
            ApproachSelectDTO approach = new ApproachSelectDTO();
            showapp.pageIndex = pageIndex;
            showapp.pageSize = pageSize;
            showapp.ApproachTime = null;
            showapp.Suppliers = textBox1.Text;
            showapp.SettlementType = textBox3.Text;
            showapp.ApproachStatus = -1;
            var content = new StringContent(JsonSerializer.Serialize<ApproachSearch>(showapp));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var aaa = info["lists"].Value<JArray>();
            var counts = info["totalCount"];
            var pages = info["pageCount"];
            totalCount = Convert.ToInt32(counts);
            pageCount = Convert.ToInt32(pages);
            List<Approach> cts = aaa.ToObject<List<Approach>>();
            dataGridView1.DataSource = cts;
            uiTextBox1.Text = pageIndex.ToString();
            uiTextBox2.Text = totalCount.ToString();
        }


        private void button2_Click(object sender, EventArgs e)
        {
            if (dt_ApproachTime.Text != null)
            {
                ApproachTime = Convert.ToDateTime(dt_ApproachTime.Text);
            }
            if (textBox1.Text != null)
            {
                Suppliers = textBox1.Text;
            }
            if (textBox3.Text != null)
            {
                SettlementType = textBox3.Text;
            }
            if (cl_Status.Text != null)
            {
                ApproachStatus = -1;
            }
            ApproachListStr();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            pageIndex = 1;
            ApproachListStr();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (pageIndex == 1)
            {
                MessageBox.Show("到首页了");
            }
            else
            {
                pageIndex -= 1;
            }
            ApproachListStr();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (pageIndex == pageCount)
            {
                MessageBox.Show("到末页了");
            }
            else
            {
                pageIndex += 1;
            }
            ApproachListStr();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            pageIndex = pageCount;
            ApproachListStr();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            ApproachListStr();
        }
    }
}
